const Exercise = require('../models/Exercise');

// 获取用户的运动信息
exports.getExerciseInfo = async (req, res) => {
  try {
    const exerciseInfo = await Exercise.findOne({ username: req.params.username });
    if (!exerciseInfo) {
      return res.status(404).json({ message: 'No exercise info found' });
    }
    res.json(exerciseInfo);
  } catch (error) {
    res.status(500).json({ message: 'Server error' });
  }
};

// 更新用户的运动信息
exports.updateExerciseInfo = async (req, res) => {
  try {
    const { exerciseType, frequency, duration, username, avatar, gender } = req.body;

    let exerciseInfo = await Exercise.findOne({ username });
    if (exerciseInfo) {
      // 更新已有的记录
      exerciseInfo.exerciseType = exerciseType;
      exerciseInfo.frequency = frequency;
      exerciseInfo.duration = duration;
      exerciseInfo.avatar = avatar;
      exerciseInfo.gender = gender;
    } else {
      // 创建新的记录
      exerciseInfo = new Exercise({
        exerciseType,
        frequency,
        duration,
        username,
        avatar,
        gender
      });
    }

    await exerciseInfo.save();
    res.json({ message: 'Exercise info updated successfully' });
  } catch (error) {
    res.status(500).json({ message: 'Server error' });
  }
};
